
/*
 * jquery 城市选择器，作者：陈晓伟
 * Copyright (c) 2006 - 2012 Softweare
 * 
 * jquery.table.js
 *
 */

(function ($) {
	$.fn.cityselector= function (opts) {
		var $this;
		var ja=[];
		ja['0100']='北京';
		ja['0200']='上海';
		ja['0300']='广东';ja['0302']='广州';ja['0303']='惠州';ja['0304']='汕头';ja['0305']='珠海';ja['0306']='佛山';ja['0307']='中山';ja['0308']='东莞';ja['0310']='从化';ja['0314']='韶关';ja['0315']='江门';ja['0316']='增城';ja['0317']='湛江';ja['0318']='肇庆';ja['0319']='清远';ja['0320']='潮州';ja['0321']='河源';ja['0322']='揭阳';ja['0323']='茂名';ja['0324']='汕尾';ja['0325']='顺德';
		ja['0400']='深圳';
		ja['0500']='天津';
		ja['0600']='重庆';
		ja['0700']='江苏';ja['0702']='南京';ja['070201']='宣武';ja['0703']='苏州';ja['0704']='无锡';ja['0705']='常州';ja['0706']='昆山';ja['0707']='常熟';ja['0708']='扬州';ja['0709']='南通';ja['0710']='镇江';ja['0711']='徐州';ja['0712']='连云港';ja['0713']='盐城';ja['0714']='张家港';
		ja['0800']='浙江';ja['0802']='杭州';ja['0803']='宁波';ja['0804']='温州';ja['0805']='绍兴';ja['0806']='金华';ja['0807']='嘉兴';ja['0808']='台州';ja['0809']='湖州';ja['0810']='丽水';ja['0811']='舟山';ja['0812']='衢州';
		ja['0900']='四川';ja['0902']='成都';ja['0903']='绵阳';ja['0904']='乐山';ja['0905']='泸州';ja['0906']='德阳';ja['0907']='宜宾';ja['0908']='自贡';ja['0909']='内江';ja['0910']='攀枝花';
		ja['1000']='海南';ja['1002']='海口';ja['1003']='三亚';
		ja['1100']='福建';ja['1102']='福州';ja['1103']='厦门';ja['1104']='泉州';ja['1105']='漳州';ja['1106']='莆田';ja['1107']='三明';ja['1108']='南平';ja['1109']='宁德';ja['1110']='龙岩';
		ja['1200']='山东';ja['1202']='济南';ja['1203']='青岛';ja['1204']='烟台';ja['1205']='潍坊';ja['1206']='威海';ja['1207']='淄博';ja['1208']='临沂';ja['1209']='济宁';ja['1210']='东营';ja['1211']='泰安';ja['1212']='日照';ja['1213']='德州';
		ja['1300']='江西';ja['1302']='南昌';ja['1303']='九江';
		ja['1400']='广西';ja['1402']='南宁';ja['1403']='桂林';ja['1404']='柳州';ja['1405']='北海';
		ja['1500']='安徽';ja['1502']='合肥';ja['1503']='芜湖';ja['1504']='安庆';ja['1505']='马鞍山';ja['1506']='蚌埠';ja['1507']='阜阳';ja['1508']='铜陵';ja['1509']='滁州';ja['1510']='黄山';ja['1511']='淮南';ja['1512']='六安';ja['1513']='巢湖';ja['1514']='宣城';ja['1515']='池州';
		ja['1600']='河北';ja['1602']='石家庄';ja['1603']='廊坊';ja['1604']='保定';ja['1605']='唐山';ja['1606']='秦皇岛';
		ja['1700']='河南';ja['1702']='郑州';ja['1703']='洛阳';ja['1704']='开封';
		ja['1800']='湖北';ja['1802']='武汉';ja['1803']='宜昌';ja['1804']='黄石';ja['1805']='襄樊';ja['1806']='十堰';ja['1807']='荆州';ja['1808']='荆门';ja['1809']='孝感';ja['1810']='鄂州';
		ja['1900']='湖南';ja['1902']='长沙';ja['1903']='株洲';ja['1904']='湘潭';ja['1905']='衡阳';ja['1906']='岳阳';ja['1907']='常德';ja['1908']='益阳';ja['1909']='郴州';ja['1910']='邵阳';ja['1911']='怀化';ja['1912']='娄底';ja['1913']='永州';ja['1914']='张家界';
		ja['2000']='陕西';ja['2002']='西安';ja['2003']='咸阳';ja['2004']='宝鸡';ja['2005']='铜川';ja['2006']='延安';
		ja['2100']='山西';ja['2102']='太原';ja['2103']='运城';ja['2104']='大同';ja['2105']='临汾';
		ja['2200']='黑龙江';ja['2202']='哈尔滨';ja['2203']='伊春';ja['2204']='绥化';ja['2205']='大庆';ja['2206']='齐齐哈尔';ja['2207']='牡丹江';ja['2208']='佳木斯';
		ja['2300']='辽宁';ja['2302']='沈阳';ja['2303']='大连';ja['2304']='鞍山';ja['2305']='营口';ja['2306']='抚顺';ja['2307']='锦州';ja['2308']='丹东';ja['2309']='葫芦岛';ja['2310']='本溪';ja['2311']='辽阳';ja['2312']='铁岭';
		ja['2400']='吉林';ja['2402']='长春';ja['2403']='吉林';ja['2404']='辽源';ja['2405']='通化';
		ja['2500']='云南';ja['2502']='昆明';ja['2503']='曲靖';ja['2504']='玉溪';ja['2505']='大理';ja['2506']='丽江';ja['2507']='蒙自';ja['2508']='开远';ja['2509']='个旧';ja['2510']='红河州';
		ja['2600']='贵州';ja['2602']='贵阳';ja['2603']='遵义';
		ja['2700']='甘肃';ja['2702']='兰州';ja['2703']='金昌';
		ja['2800']='内蒙';ja['2802']='呼和浩特';ja['2803']='赤峰';ja['2804']='包头';
		ja['2900']='宁夏';ja['2902']='银川';
		ja['3000']='西藏';ja['3002']='拉萨';ja['3003']='日喀则';
		ja['3100']='新疆';ja['3102']='乌鲁木齐';ja['3103']='克拉玛依';ja['3104']='喀什地区';
		ja['3200']='青海';ja['3202']='西宁';
		ja['3300']='香港';
		ja['3400']='澳门';
		ja['3500']='台湾';
		ja['3600']='国外';
		//主要城数据字典
		 
		//所有省份数据字典
		var allprov=['1600','2100','2800','2300','2400','2200','0700','0800','1500','1100','1300','1200','0300','1400','1000','1700','1800','1900','2000','2700','3200','2900','3100','0900','2600','2500','3000','3300','3400','3500','3600'];
		
		var defaults={
			onSelect:null
		};
		var param = $.extend(defaults, opts);
		this.each(function () {
			$this=$(this);
			$this.addClass('citySelector');
			
			//init frame
			var html='<div class="label"><a>关闭</a></div><div class="label"><span>省</span><span>市</span><span>县</span></div>';
	   		html+='<div class="sheng"></div>';
	    	html+='<div class="shi" style="display: inline-block;;"></div>';
	    	html+='<div class="xian" style="display: inline-block;;"></div>';
			$this.html(html);
			//init data
			html="<ul>";
			for(var i=0;i<allprov.length;i++){
				var key=allprov[i];
				html+="<li key='"+key+"'>"+ja[key]+"</li>";
			}
			html+="</ul>";
			$this.find('.sheng').html(html);;
			$this.find('.sheng ul li').click(function(){
				var html="<ul>";
				for(var i=1;i<100;i++){
					var key=$(this).attr('key').substring(0,2);
					if(i<10){
						key=key+"0"+i;
					}else{
						key=key+i;
					}
					if(ja[key]){
						html+="<li key='"+key+"'>"+ja[key]+"</li>";
					}
				}
				html+="</ul>";
				$this.find('.shi').html(html);;
//				if(param.onSelect){
//					$this.find('.shi ul li').click(function(){
//						param.onSelect($(this).attr('key'),$(this).text());
//						$this.hide();
//						$("#citySelectorBg").remove();
//					});
//				}
				$('.sheng ul li.current').removeClass('current');
				$(this).addClass('current');
			});
			
			$this.find('.shi ul li').click(function(){
				var html="<ul>";
				for(var i=1;i<100;i++){
					var key=$(this).attr('key').substring(0,4);
				
					if(i<10){
						key=key+"0"+i;
					}else{
						key=key+i;
					}
					if(ja[key]){
						html+="<li key='"+key+"'>"+ja[key]+"</li>";
					}
				}
				html+="</ul>";
				$this.find('.xian').html(html);;
//				if(param.onSelect){
//					$this.find('.shi ul li').click(function(){
//						param.onSelect($(this).attr('key'),$(this).text());
//						$this.hide();
//						$("#citySelectorBg").remove();
//					});
//				}
				$('.shi ul li.current').removeClass('current');
				$(this).addClass('current');
			});
		});
		
	 	this.showSelector=function(){
	 		$('<div id="citySelectorBg"/>').appendTo("body");
	 		var width=$('#citySelectorBg').width()-$this.width();
			$this.css('left',width/2+"px");
		 	var height=$('#citySelectorBg').height()-$this.height()/2;
		 
		 	$this.css('top',height/3+"px");
		 	$this.css('display',"block");
		 	
	 	};
	 	this.hideSelector=function(){
	 		$("#citySelectorBg").remove();
	 		$this.css('display',"none");
	 	}
		return this;
	};
})(jQuery);

